﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Utilities.QuickSql;
using MySql.Data;

namespace Deviseur_Cs2i_Car
{
    public partial class AdminVoiMotor : Form
    {
        public AdminVoiMotor()
        {
            InitializeComponent();

            Thread VoiMotorThread = new Thread(new ThreadStart(getAllVoiMotor_Thread));
            VoiMotorThread.Start();
        }

        private void getAllVoiMotor_Thread()
        {
            Invoke(new MethodInvoker(getAllVoiMotor));         
        }

        private void getAllVoiMotor()
        {
            Table VoiMotorVoiture = Program.Db.QueryTable("SELECT V.NomVoi, M.ChMotor, TM.LibelleTypeMoteur, H.LibelleHTDI, C.LibelleContenance, V.NumVoi, M.NumMotor FROM Voi_Motor VO INNER JOIN MOTORISATION M ON M.NumMotor = VO.NumMotor INNER JOIN VOITURE V ON V.NumVoi = VO.NumVoi INNER JOIN TYPEMOTEUR TM ON M.NumTypeMoteur = TM.NumTypeMoteur LEFT OUTER JOIN HTDI H ON H.NumHTDI = TM.NumHTDI INNER JOIN CONTENANCE C ON C.NumContenance = M.NumContenance");
            
            this.dataGridView1.Rows.Clear();

            foreach (Record r in VoiMotorVoiture)
                this.dataGridView1.Rows.Add(r.Values[0], r.Values[1], r.Values[2], r.Values[3],r.Values[4],r.Values[5],r.Values[6]);
        }

        private void buttonAddVoiMotor_Click(object sender, EventArgs e)
        {
            AddVoiMotor FenAddVoiMotor = new AddVoiMotor();
            FenAddVoiMotor.ShowDialog();
        }

        private void AdminVoiMotor_Activated(object sender, EventArgs e)
        {
            Thread VoiMotorThread = new Thread(new ThreadStart(getAllVoiMotor_Thread));
            VoiMotorThread.Start();
        }

        private void buttonDelVoiMotor_Click(object sender, EventArgs e)
        {
            VoiMotor delVoiMotor = new VoiMotor();
            delVoiMotor.delete_VoiMotor(Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[5].Value.ToString()), Convert.ToDouble(this.dataGridView1.SelectedRows[0].Cells[6].Value.ToString()));
            Thread VoiMotorThread = new Thread(new ThreadStart(getAllVoiMotor_Thread));
            VoiMotorThread.Start();
        }

        private void buttonUpdateVoiMotor_Click(object sender, EventArgs e)
        {
            AddVoiMotor FenAddVoiMotor = new AddVoiMotor(Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[5].Value.ToString()), Convert.ToDouble(this.dataGridView1.SelectedRows[0].Cells[6].Value.ToString()));
            FenAddVoiMotor.ShowDialog();
            // Coucou
        }

        private void buttonModifVoiMotor_Click(object sender, EventArgs e)
        {
            AddVoiMotor FenAddVoiMotor = new AddVoiMotor(Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[5].Value.ToString()), Convert.ToDouble(this.dataGridView1.SelectedRows[0].Cells[6].Value.ToString()));
            FenAddVoiMotor.ShowDialog();
        }
    }
}
